package com.itheima.pinda.controller.scope;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.pinda.DTO.entity.PdCoreOrg;
import com.itheima.pinda.DTO.entity.pojo.agency.PdAgencyScope;
import com.itheima.pinda.feign.OrgFeign;
import com.itheima.pinda.feign.service.agency.PdAgencyScopeService;
import com.itheima.pinda.vo.base.angency.AgencyScopeVos;
import com.itheima.pinda.vo.base.angency.AgencyVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 机构业务范围表 前端控制器
 * </p>
 *
 * @author xsf
 * @since 2021-11-24
 */
@RestController
@RequestMapping("/base/scope")
public class ScopeController {

    @Autowired
    private OrgFeign orgFeign;

    @Autowired
    private PdAgencyScopeService pdAgencyScopeService;

    /**
     *
     * 通过组织id查询作业范围
     * @param id
     * @return
     */
    @GetMapping("/scope")
    public AgencyScopeVos findAllAgencyScope(@RequestParam(name = "id") String id) {
        AgencyScopeVos agencyScopeVo = new AgencyScopeVos();

        PdCoreOrg pdCoreOrg = orgFeign.queryById(id);


        if(!ObjectUtils.isEmpty(pdCoreOrg)){
            AgencyVo agencyVo = new AgencyVo();
            BeanUtils.copyProperties(pdCoreOrg,agencyVo);
            agencyScopeVo.setAgency(agencyVo);

            QueryWrapper<PdAgencyScope> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("agency_id",pdCoreOrg.getId());
            List<PdAgencyScope> list = pdAgencyScopeService.list(queryWrapper);
            agencyScopeVo.setAreas(list);
        }

        return agencyScopeVo;
    }

    /**
     * 添加机构时添加机构作业范围
     * @param pdAgencyScopeList
     * @return
     */
    @PostMapping("/addPdAgencyScopes")
    public Boolean addPdAgencyScopes(@RequestBody List<PdAgencyScope> pdAgencyScopeList){
        return pdAgencyScopeService.saveBatch(pdAgencyScopeList);
    }

}
